<?php

namespace app\api\model;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'express_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'state_text',
        'status_text',
        'is_open_text',
        'is_delivery_text'
    ];
    

    
    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1'), '2' => __('State 2'), '3' => __('State 3'), '4' => __('State 4')];
    }
    public function getStatusList()
    {
        return [
            'created' => __('Status created'),
            'complete' => __('Status complete'),
            'waitingpay' => __('Status waitingpay'),
            'successpay' => __('Status successpay'),
        ];
    }


    public function getIsOpenList()
    {
        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
    }

    public function getIsDeliveryList()
    {
        return ['0' => __('Is_delivery 0'), '1' => __('Is_delivery 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function getIsOpenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
        $list = $this->getIsOpenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsDeliveryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_delivery']) ? $data['is_delivery'] : '');
        $list = $this->getIsDeliveryList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function address()
    {
        return $this->belongsTo('OrderAddress', 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
    }
    public function goods(){
        return $this->hasMany('OrderGoods','order_id','id');
    }

    public function setOrderNoAttr($value,$data){
        return 'AD'.substr(str_shuffle(substr(time(),-8).substr(substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).$value,-8)),0,8);
    }

    // 获取订单支付信息
    public function getPayOrder($order_ids){
        $orderList = model('app\api\model\Order')
            ->with(['goods'])
            ->where('id','in',$order_ids)
            ->where('status','waitingpay')
            ->select();
        if(!$orderList){
            $this->error = '订单不存在';
            return false;
        }
        $priceSum = $weightSum = $servicePrice = 0;
        foreach ($orderList as $order){
            $weightSum += $order['delivery_weight'];
            $priceSum += $order['delivery_price'];
            $servicePrice += model('app\api\model\Service')->where('id','in',$order['service_ids'])->sum('price');
        }
        $price = $priceSum;
        $discountNow = model('app\api\model\Discount')->where([
            'weight' => ['<',$weightSum]
        ])->order('weight asc')->find();
        if($discountNow){
            $price = bcmul($priceSum,bcdiv($discountNow['discount'],10,2),2);
        }
        $discountPrice = bcsub($priceSum,$price,2);
        $discountNext = model('app\api\model\Discount')->where([
            'weight' => ['>',$weightSum]
        ])->order('weight asc')->find();

        $data = [
            'orderList' => $orderList, // 订单信息
            'priceSum' => $priceSum, // 订单金额
            'servicePrice' => $servicePrice, // 订单金额
            'weightSum' => $weightSum, // 订单重量(Kg)
            'price' => $price + $servicePrice, // 支付金额
            'discountPrice' => $discountPrice, // 优惠金额
            'discountNow' => $discountNow, // 当前阶段优惠折扣
            'discountNext' => $discountNext, // 下阶段优惠折扣
        ];
        return $data;
    }
}
